package com.huiyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.huiyuan.common.*;
import com.huiyuan.domain.Order;
import com.huiyuan.feign.BikeService;
import com.huiyuan.feign.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.huiyuan.mapper.OrderMapper;
import com.huiyuan.service.OrderService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author huiyuan080
 * @data 2023/5/2
 * @apiNote
 */
@Service
public class OrderServiceImpl implements OrderService {


    @Autowired
    private MemberService memberService;

    @Autowired
    private BikeService bikeService;


    @Resource
    private OrderMapper orderMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return orderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Order record) {
        return orderMapper.insert(record);
    }

    @Override
    public int insertSelective(Order record) {
        return orderMapper.insertSelective(record);
    }

    @Override
    public Order selectByPrimaryKey(Integer id) {
        return orderMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Order record) {
        return orderMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Order record) {
        return orderMapper.updateByPrimaryKey(record);
    }

    /**
     * 添加订单
     */
    @Override
    public int useBicycle(Integer userId, Integer bicycleId) {
        Bike bike = bikeService.isNull(bicycleId);
        if (bike == null || "1".equals(bike.getBikeAvailable()) || "1".equals(bike.getBikeState())) {
            return 2;
        }
        User user = memberService.isNull(userId);
        if (user == null || "1".equals(user.getUserIsRent())) {
            return 3;
        }
        //修改 用户的租车状态
        memberService.addUse(userId);
        //修改单车的使用状态
        bikeService.addUse(bicycleId);
        Order build = Order.builder().id(userId).bikeId(bicycleId).createTime(new Date()).build();
        return orderMapper.insertSelective(build);
    }


    /**
     * 分页查询会员用车的记录
     *
     * @param page
     * @return
     */
    @Override
    public PageVo getMemberOrder(PageVo page, Integer userId) {
        com.github.pagehelper.Page<Object> pageHelper = PageHelper.startPage(page.getPageNumber(), page.getPageSize());
        this.orderMapper.getPage(userId);
        page.setList(pageHelper.getResult());
        page.setTotalCount(pageHelper.getTotal());
        User user = memberService.isNull(userId);
        page.setUserIntegral(user.getUserIntegral());
        return page;
    }


    /**
     * 会员锁车还车
     *
     * @param userId
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int backBicycle(Integer userId, Integer id) {
        User user = memberService.isNull(userId);
        Order order = orderMapper.selectByPrimaryKey(id);
        order.setEndTime(new Date());
        long time = order.getEndTime().getTime() - order.getCreateTime().getTime();
        int ceil = (int) Math.ceil((double) time / 60000);
        Integer userIntegral = user.getUserIntegral();
        //需要支付的金额
        double amt = ceil * 1.5;
        order.setPrice(new BigDecimal(amt));
        //这是我的积分可换的金额
        double floor = 0.00;
        if (userIntegral > 0) {
            //判断积分是否为双数
            int i = userIntegral % 2;
            if (i == 1) {
                floor = (userIntegral - 1) / 2 * 0.5;
            } else {
                floor = userIntegral / 2 * 0.5;
            }
            if (floor >= amt) {
                //如果积分兑换的余额大于车费
                String s = String.valueOf((floor - amt) / 0.5 * 2);
                user.setUserIntegral(Integer.parseInt(s) + 1);
            } else {
                //积分兑换的金额小于待支付的金额
                BigDecimal bigDecimal = new BigDecimal("0");
                BigDecimal bigDecimal1 = new BigDecimal(amt - floor);
                int flag = user.getUserBalance().compareTo(bigDecimal1);
                if (flag >= 0) {
                    bigDecimal = user.getUserBalance().subtract(new BigDecimal(amt - floor));
                } else {
                    throw new RuntimeException("余额不足");
                }
                user.setUserBalance(bigDecimal);
                user.setUserIntegral(1);
            }
        } else {
            //没有积分  直接进行加减
            BigDecimal bigDecimal = new BigDecimal("0");
            BigDecimal bigDecimal1 = new BigDecimal(amt);
            int flag = bigDecimal.compareTo(bigDecimal1);
            if (flag >= 0) {
                user.setUserBalance(user.getUserBalance().subtract(new BigDecimal(amt)));
            } else {
                throw new RuntimeException("余额不足");
            }
        }
        user.setUserIsRent("0");
        memberService.update(user);
        bikeService.addUse(order.getBikeId());
        return orderMapper.updateByPrimaryKeySelective(order);
    }


}
